package com.eenie.common.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.eenie.common.R;


public class RatioRelativeLayout extends RelativeLayout {

    private float mRatio = 1.0f;


    public RatioRelativeLayout(@NonNull Context context) {
        super(context);
    }

    public RatioRelativeLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.RatioRelativeLayout, 0, 0);
        mRatio = array.getFloat(R.styleable.RatioRelativeLayout_cus_relative_ratio, 1.0f);
        array.recycle();
    }

    public RatioRelativeLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.RatioRelativeLayout, defStyleAttr, 0);
        mRatio = array.getFloat(R.styleable.RatioRelativeLayout_cus_relative_ratio, 1.0f);
        array.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) (MeasureSpec.getSize(widthMeasureSpec) * mRatio), MeasureSpec.getMode(widthMeasureSpec)));
    }
}
